package com.opengroupe.utils;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.opengroupe.dto.CandidatDto;
import com.opengroupe.dto.CategorieDto;
import com.opengroupe.dto.QuestionDto;
import com.opengroupe.dto.QuestionnaireDto;
import com.opengroupe.dto.ReponseDto;
import com.opengroupe.entities.Candidat;
import com.opengroupe.entities.Categorie;
import com.opengroupe.entities.Question;
import com.opengroupe.entities.Reponse;
import com.opengroupe.service.common.CommonService;
import com.opengroupe.service.common.CrudService;
import com.opengroupe.util.HeavyResultatQuestionnaire;

public class QrhWsUtils {

	@Autowired
	private CrudService crudService;
	
	@Autowired
	private CommonService commonService;


	/**
	 * Construit "manuellement" un {@link QuestionnaireDto} sous forme de JSON
	 * @param emailCandidat
	 * @param libelleCategorie
	 * @return
	 */
	@Transactional
	public synchronized String buildQuestionnaire(String emailCandidat, String libelleCategorie) {
		
		Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
		
		QuestionnaireDto questionnaireDto = new QuestionnaireDto();
		
		Candidat candidat = (Candidat) crudService.read(emailCandidat, Candidat.class.getCanonicalName());
		
		CandidatDto candidatDto = new CandidatDto();
		candidatDto = convertCandidatToCandidatDto(candidat);
		
		Categorie categorie = (Categorie) crudService.read(libelleCategorie, Categorie.class.getCanonicalName());
		List<Question> questions = categorie.getQuestions();
		
		List<QuestionDto> questionDtos = new ArrayList<QuestionDto>();
		for(Question question : questions){
			questionDtos.add(convertQuestionToQuestionDto(question));
		}
		
		questionnaireDto.setQuestions(questionDtos);
		questionnaireDto.setCandidat(candidatDto);
		
		return gson.toJson(questionnaireDto, QuestionnaireDto.class);
		
	}
	
	@Transactional
	public synchronized String consulterResultatJson(Candidat candidat){

		HeavyResultatQuestionnaire heavyResultatQuestionnaire = commonService.consulterResultat(candidat);
		if(heavyResultatQuestionnaire == null){
			return "";
		}
		
		return heavyResultatQuestionnaire.getLightResultatQuestionnaire();
	}
	
	public static CandidatDto convertCandidatToCandidatDto(Candidat candidat) {
		CandidatDto candidatDto = new CandidatDto();
		candidatDto.setIdCandidat(candidat.getIdCandidat());
		candidatDto.setEmailCandidat(candidat.getEmailCandidat());
		candidatDto.setNomCandidat(candidat.getNomCandidat());
		candidatDto.setPrenomCandidat(candidat.getPrenomCandidat());
		candidatDto.setTelephoneCandidat(candidat.getTelephoneCandidat());
		return candidatDto;
	}
	
	public static Candidat convertCandidatDtoToCandidat(CandidatDto candidatDto) {
		Candidat candidat = new Candidat();
		candidat.setNomCandidat(candidatDto.getNomCandidat());
		candidat.setPrenomCandidat(candidatDto.getPrenomCandidat());
		candidat.setEmailCandidat(candidatDto.getEmailCandidat());
		candidat.setTelephoneCandidat(candidatDto.getTelephoneCandidat());
		return candidat;
	}
	
	@Transactional
	public static QuestionDto convertQuestionToQuestionDto(Question question) {
		QuestionDto questionDto = new QuestionDto();
		questionDto.setCategorie(convertCategorieToCategorieDto(question.getCategorie()));
		questionDto.setContenuQuestion(question.getContenuQuestion());
		questionDto.setIdQuestion(question.getIdQuestion());
		questionDto.setReponsesCorrectes(getReponseDtoList(question.getReponsesCorrectes()));
		questionDto.setReponsesEventuelles(getReponseDtoList(question.getReponsesEventuelles()));
		return questionDto;
	}
	
	/**
	 * Creates a {@link Question} with {@linkplain contenuQuestion} and {@link Categorie} from a {@link QuestionDto} 
	 * @param questionDto
	 * @return {@link Question}
	 */
	public Question convertQuestionDtoToQuestion(QuestionDto questionDto) {
		Question question = new Question();
		
		question.setContenuQuestion(questionDto.getContenuQuestion());
		Categorie categorie = (Categorie) crudService.read(questionDto.getCategorie().getLibelleCategorie(), Categorie.class.getCanonicalName());
		question.setCategorie(categorie);
		
		return question;
	}
	
	@Transactional
	public static List<ReponseDto> getReponseDtoList(List<Reponse> reponses) {
		List<ReponseDto> reponseDtos = new ArrayList<ReponseDto>();
		for(Reponse reponse : reponses){
			reponseDtos.add(convertReponseToReponseDto(reponse));
		}
		return reponseDtos;
	}
	@Transactional
	public static ReponseDto convertReponseToReponseDto(Reponse reponse) {
		ReponseDto reponseDto = new ReponseDto();
		reponseDto.setContenuReponse(reponse.getContenuReponse());
		reponseDto.setIdReponse(reponse.getIdReponse());
		return reponseDto;
	}

	public static CategorieDto convertCategorieToCategorieDto(Categorie categorie) {
		CategorieDto categorieDto = new CategorieDto();
		categorieDto.setIdCategorie(categorie.getIdCategorie());
		categorieDto.setLibelleCategorie(categorie.getLibelleCategorie());
		return categorieDto;
	}
	
	
}
